import {
  validatorPhone,
  validatorEmail,
  validatorPass,
  validatorNumber,
} from '@/utils/validator'
import { FormItemType } from './type'
import { FormItemRule } from 'element-plus'
const createRules = (data: FormItemType[], field: any) => {
  data.map((item) => {
    // 检测规则是一个数组类型 Array
    const rules_array: FormItemRule[] = []
    if (item.required) {
      rules_array.push({
        required: true,
        message: item.message || createMessage(item),
      })
    }
    // 校验手机号
    if (item.value_type && item.value_type === 'phone') {
      const rule = { validator: validatorPhone, trigger: 'change' }
      rules_array.push(rule)
    }
    // 校验邮箱
    if (item.value_type && item.value_type === 'email') {
      const rule = { validator: validatorEmail, trigger: 'change' }
      rules_array.push(rule)
    }
    // 校验邮箱
    if (item.value_type && item.value_type === 'number') {
      const rule = { validator: validatorNumber, trigger: 'change' }
      rules_array.push(rule)
    }
    // 校验密码
    if (item.value_type && item.value_type === 'password') {
      const validatorPassword = (rule: any, value: any, callback: any) => {
        if (!value) {
          callback(new Error('请输入密码'))
        } else if (!validatorPass(value)) {
          callback(new Error('请输入输入6~18的数字、字母组合'))
        } else {
          callback()
        }
      }
      const rule = { validator: validatorPassword, trigger: 'change' }
      rules_array.push(rule)
    }
    if (item.value_type && item.value_type === 'passwords') {
      const p1 = data.filter((item) => item.value_type === 'password')[0]
      const validatorPassword = (rule: any, value: any, callback: any) => {
        const p1_value = field[p1.prop]
        if (!value) {
          callback(new Error('请输入密码'))
        } else if (!validatorPass(value)) {
          callback(new Error('请输入输入6~18的数字、字母组合'))
        } else if (p1_value && p1_value !== value) {
          callback(new Error('确认密码不一致'))
        } else {
          callback()
        }
      }
      const rule = { validator: validatorPassword, trigger: 'change' }
      rules_array.push(rule)
    }
    // 是否有额外的校验规则
    if (item.rules && Array.isArray(item.rules) && item.rules.length > 0) {
      item.rules = [...item.rules, ...rules_array]
    } else {
      item.rules = rules_array
    }
  })
  return data
}

const createMessage = (data: FormItemType) => {
  let msg = ''
  switch (data.type) {
    case 'input':
    case 'textarea':
      msg = '请输入'
      break
    case 'select':
    case 'checkbox':
    case 'radio':
    case 'radioBtn':
      msg = '请选择'
      break
    case 'upload':
      msg = '请上传'
      break
  }
  if (!data.label) return data.placeholder
  return `${msg}${data.label}`
}
export default createRules
